# Copyright 2025 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load(
    "//pw_build:compatibility.bzl",
    "incompatible_with_mcu",
    "minimum_cxx_20",
)
load("//pw_build:pw_facade.bzl", "pw_facade")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
)

licenses(["notice"])

cc_library(
    name = "poll",
    hdrs = [
        "public/pw_async2/internal/poll_internal.h",
        "public/pw_async2/poll.h",
        "public/pw_async2/try.h",
    ],
    strip_include_prefix = "public",
    deps = [
        "//pw_polyfill",
        "//pw_string:to_string",
        "//third_party/fuchsia:stdcompat",
    ],
)

pw_cc_test(
    name = "poll_test",
    srcs = ["poll_test.cc"],
    deps = [
        ":poll",
        "//pw_result",
    ],
)

pw_facade(
    name = "dispatcher",
    srcs = [
        "context.cc",
        "dispatcher_base.cc",
        "task.cc",
        "waker.cc",
    ],
    hdrs = [
        "public/pw_async2/context.h",
        "public/pw_async2/dispatcher.h",
        "public/pw_async2/dispatcher_base.h",
        "public/pw_async2/future.h",
        "public/pw_async2/internal/config.h",
        "public/pw_async2/lock.h",
        "public/pw_async2/owned_task.h",
        "public/pw_async2/task.h",
        "public/pw_async2/waker.h",
    ],
    backend = ":dispatcher_backend",
    # LINT.IfChange
    defines = select({
        ":debug_wait_reason_disabled": ["PW_ASYNC2_DEBUG_WAIT_REASON=0"],
        ":debug_wait_reason_enabled": ["PW_ASYNC2_DEBUG_WAIT_REASON=1"],
    }),
    # LINT.ThenChange(//pw_async2/public/pw_async2/internal/config.h)
    implementation_deps = [
        "//pw_assert:check",
        "//pw_thread:sleep",
    ],
    strip_include_prefix = "public",
    deps = [
        ":config_override",
        ":poll",
        "//pw_assert:assert",
        "//pw_chrono:system_clock",
        "//pw_containers:intrusive_forward_list",
        "//pw_containers:intrusive_list",
        "//pw_log",
        "//pw_log:args",
        "//pw_metric:metric",
        "//pw_sync:interrupt_spin_lock",
        "//pw_sync:lock_annotations",
        "//pw_tokenizer",
        "//pw_toolchain:no_destructor",
    ],
)

label_flag(
    name = "dispatcher_backend",
    build_setting_default = ":dispatcher_backend_multiplexer",
)

label_flag(
    name = "config_override",
    build_setting_default = "//pw_build:default_module_config",
)

constraint_setting(
    name = "debug_wait_reason",
    default_constraint_value = "debug_wait_reason_enabled",
)

constraint_value(
    name = "debug_wait_reason_disabled",
    constraint_setting = ":debug_wait_reason",
)

constraint_value(
    name = "debug_wait_reason_enabled",
    constraint_setting = ":debug_wait_reason",
)

cc_library(
    name = "dispatcher_backend_multiplexer",
    visibility = ["//targets:__pkg__"],
    deps = select({
        "@platforms//os:linux": ["//pw_async2_epoll:dispatcher"],
        "//conditions:default": ["//pw_async2_basic:dispatcher"],
    }),
)

pw_cc_test(
    name = "dispatcher_test",
    srcs = ["dispatcher_test.cc"],
    deps = [
        ":dispatcher",
        "//pw_containers:vector",
    ],
)

pw_cc_test(
    name = "task_test",
    srcs = ["task_test.cc"],
    deps = [
        ":dispatcher",
        "//pw_sync:binary_semaphore",
        "//pw_thread:sleep",
        "//pw_thread:test_thread_context",
        "//pw_thread:thread",
    ],
)

cc_library(
    name = "pendable",
    hdrs = [
        "public/pw_async2/pendable.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        ":poll",
    ],
)

pw_cc_test(
    name = "pendable_test",
    srcs = ["pendable_test.cc"],
    deps = [
        ":dispatcher",
        ":pendable",
        ":poll",
    ],
)

pw_cc_test(
    name = "future_test",
    srcs = ["future_test.cc"],
    has_nc_test = True,
    deps = [
        ":dispatcher",
        ":pend_func_task",
        ":poll",
        ":select",
    ],
)

cc_library(
    name = "value_future",
    hdrs = [
        "public/pw_async2/value_future.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        ":poll",
    ],
)

pw_cc_test(
    name = "value_future_test",
    srcs = ["value_future_test.cc"],
    deps = [
        ":dispatcher",
        ":pend_func_task",
        ":poll",
        ":value_future",
    ],
)

cc_library(
    name = "pend_func_awaitable",
    hdrs = [
        "public/pw_async2/pend_func_awaitable.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        ":poll",
        "//pw_function",
    ],
)

pw_cc_test(
    name = "pend_func_awaitable_test",
    srcs = ["pend_func_awaitable_test.cc"],
    deps = [
        ":coro",
        ":coro_or_else_task",
        ":dispatcher",
        ":pend_func_awaitable",
        ":poll",
        "//pw_allocator:testing",
        "//pw_function",
        "//pw_status",
    ],
)

cc_library(
    name = "pend_func_task",
    hdrs = [
        "public/pw_async2/pend_func_task.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        "//pw_function",
    ],
)

pw_cc_test(
    name = "pend_func_task_test",
    srcs = ["pend_func_task_test.cc"],
    deps = [
        ":dispatcher",
        ":pend_func_task",
        "//pw_function",
    ],
)

cc_library(
    name = "callback_task",
    hdrs = [
        "public/pw_async2/callback_task.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        "//pw_function",
    ],
)

pw_cc_test(
    name = "callback_task_test",
    srcs = ["callback_task_test.cc"],
    deps = [
        ":callback_task",
        ":dispatcher",
        ":value_future",
        "//pw_function",
    ],
)

cc_library(
    name = "pendable_as_task",
    hdrs = [
        "public/pw_async2/pendable_as_task.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
    ],
)

pw_cc_test(
    name = "pendable_as_task_test",
    srcs = ["pendable_as_task_test.cc"],
    deps = [
        ":dispatcher",
        ":pendable_as_task",
    ],
)

cc_library(
    name = "allocate_task",
    hdrs = [
        "public/pw_async2/allocate_task.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        "//pw_allocator:allocator",
    ],
)

pw_cc_test(
    name = "allocate_task_test",
    srcs = ["allocate_task_test.cc"],
    deps = [
        ":allocate_task",
        "//pw_allocator:testing",
    ],
)

cc_library(
    name = "once_sender",
    hdrs = [
        "public/pw_async2/once_sender.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        "//pw_function",
        "//pw_sync:mutex",
    ],
)

pw_cc_test(
    name = "once_sender_test",
    srcs = [
        "once_sender_test.cc",
    ],
    deps = [
        ":dispatcher",
        ":once_sender",
        "//pw_containers:vector",
    ],
)

cc_library(
    name = "waker_queue",
    srcs = [
        "waker_queue.cc",
    ],
    hdrs = [
        "public/pw_async2/waker_queue.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        "//pw_containers:inline_queue",
        "//pw_log:args",
    ],
)

pw_cc_test(
    name = "waker_queue_test",
    srcs = [
        "waker_queue_test.cc",
    ],
    deps = [
        ":dispatcher",
        ":pend_func_task",
        ":waker_queue",
    ],
)

cc_library(
    name = "coro",
    srcs = [
        "coro.cc",
    ],
    hdrs = [
        "public/pw_async2/coro.h",
    ],
    implementation_deps = [
        "//pw_log",
    ],
    strip_include_prefix = "public",
    target_compatible_with = minimum_cxx_20(),
    deps = [
        ":dispatcher",
        "//pw_allocator:allocator",
        "//pw_function",
        "//pw_status",
    ],
)

pw_cc_test(
    name = "coro_test",
    srcs = ["coro_test.cc"],
    deps = [
        ":coro",
        ":dispatcher",
        "//pw_allocator:null_allocator",
        "//pw_allocator:testing",
        "//pw_status",
    ],
)

cc_library(
    name = "coro_or_else_task",
    hdrs = [
        "public/pw_async2/coro_or_else_task.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":coro",
        ":dispatcher",
        "//pw_function",
        "//pw_status",
    ],
)

pw_cc_test(
    name = "coro_or_else_task_test",
    srcs = ["coro_or_else_task_test.cc"],
    deps = [
        ":coro",
        ":coro_or_else_task",
        ":dispatcher",
        "//pw_allocator:null_allocator",
        "//pw_allocator:testing",
        "//pw_status",
    ],
)

cc_library(
    name = "time_provider",
    srcs = [
        "time_provider.cc",
    ],
    hdrs = [
        "public/pw_async2/time_provider.h",
    ],
    implementation_deps = ["//pw_assert:check"],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        "//pw_chrono:virtual_clock",
        "//pw_containers:intrusive_list",
        "//pw_sync:interrupt_spin_lock",
        "//pw_sync:lock_annotations",
        "//pw_toolchain:no_destructor",
    ],
)

cc_library(
    name = "system_time_provider",
    srcs = [
        "system_time_provider.cc",
    ],
    hdrs = [
        "public/pw_async2/system_time_provider.h",
    ],
    implementation_deps = [
        "//pw_chrono:system_timer",
        "//pw_toolchain:no_destructor",
    ],
    strip_include_prefix = "public",
    deps = [
        ":time_provider",
        "//pw_chrono:system_clock",
    ],
)

pw_cc_test(
    name = "system_time_provider_test",
    srcs = [
        "system_time_provider_test.cc",
    ],
    deps = [":system_time_provider"],
)

cc_library(
    name = "simulated_time_provider",
    hdrs = [
        "public/pw_async2/simulated_time_provider.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":time_provider",
        "//pw_sync:interrupt_spin_lock",
    ],
)

pw_cc_test(
    name = "simulated_time_provider_test",
    srcs = [
        "simulated_time_provider_test.cc",
    ],
    deps = [
        ":simulated_time_provider",
        "//pw_chrono:system_clock",
    ],
)

cc_library(
    name = "cancellable_task",
    hdrs = [
        "public/pw_async2/cancellable_task.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
    ],
)

pw_cc_test(
    name = "cancellable_task_test",
    srcs = [
        "cancellable_task_test.cc",
    ],
    deps = [
        ":cancellable_task",
        ":dispatcher",
        ":pendable",
        ":poll",
        "//pw_function",
    ],
)

cc_library(
    name = "enqueue_heap_func",
    hdrs = [
        "public/pw_async2/enqueue_heap_func.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
    ],
)

pw_cc_test(
    name = "enqueue_heap_func_test",
    srcs = [
        "enqueue_heap_func_test.cc",
    ],
    deps = [
        ":dispatcher",
        ":enqueue_heap_func",
    ],
)

cc_library(
    name = "join",
    hdrs = [
        "public/pw_async2/join.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
    ],
)

pw_cc_test(
    name = "join_test",
    srcs = [
        "join_test.cc",
    ],
    has_nc_test = True,
    deps = [
        ":dispatcher",
        ":join",
        ":value_future",
    ],
)

cc_library(
    name = "select",
    hdrs = [
        "public/pw_async2/select.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        "//pw_containers:optional_tuple",
    ],
)

pw_cc_test(
    name = "select_test",
    srcs = [
        "select_test.cc",
    ],
    deps = [
        ":dispatcher",
        ":pendable",
        ":poll",
        ":select",
        ":value_future",
    ],
)

cc_library(
    name = "channel",
    hdrs = [
        "public/pw_async2/channel.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        ":poll",
        "//pw_allocator",
        "//pw_containers:deque",
        "//pw_numeric:checked_arithmetic",
        "//pw_sync:interrupt_spin_lock",
    ],
)

pw_cc_test(
    name = "channel_test",
    srcs = [
        "channel_test.cc",
    ],
    deps = [
        ":channel",
        ":dispatcher",
        ":pend_func_task",
        ":poll",
        "//pw_allocator:testing",
        "//pw_containers:vector",
    ],
)

pw_cc_test(
    name = "channel_coro_test",
    srcs = [
        "channel_coro_test.cc",
    ],
    deps = [
        ":channel",
        ":coro",
        ":coro_or_else_task",
        ":dispatcher",
        ":poll",
        "//pw_allocator:testing",
        "//pw_containers:vector",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "backends.rst",
        "code_size.rst",
        "coroutines.rst",
        "docs.rst",
        "futures.rst",
        "guides.rst",
        "informed_poll.rst",
        "public/pw_async2/coro.h",
        "quickstart.rst",
        "//pw_async2/codelab:docs",
        "//pw_async2/examples:channel.cc",
        "//pw_async2/examples:custom_future.cc",
        "//pw_async2/examples:docs",
        "//pw_async2/size_report:full_size_report",
        "//pw_async2/size_report:once_sender_size_report",
        "//pw_async2/size_report:utilities_size_report",
    ],
    prefix = "pw_async2/",
    target_compatible_with = incompatible_with_mcu(),
)

filegroup(
    name = "doxygen",
    srcs = [
        "public/pw_async2/allocate_task.h",
        "public/pw_async2/callback_task.h",
        "public/pw_async2/cancellable_task.h",
        "public/pw_async2/channel.h",
        "public/pw_async2/context.h",
        "public/pw_async2/coro.h",
        "public/pw_async2/coro_or_else_task.h",
        "public/pw_async2/dispatcher.h",
        "public/pw_async2/dispatcher_base.h",
        "public/pw_async2/enqueue_heap_func.h",
        "public/pw_async2/future.h",
        "public/pw_async2/internal/config.h",
        "public/pw_async2/join.h",
        "public/pw_async2/lock.h",
        "public/pw_async2/once_sender.h",
        "public/pw_async2/owned_task.h",
        "public/pw_async2/pend_func_awaitable.h",
        "public/pw_async2/pend_func_task.h",
        "public/pw_async2/pendable.h",
        "public/pw_async2/pendable_as_task.h",
        "public/pw_async2/poll.h",
        "public/pw_async2/select.h",
        "public/pw_async2/simulated_time_provider.h",
        "public/pw_async2/system_time_provider.h",
        "public/pw_async2/task.h",
        "public/pw_async2/time_provider.h",
        "public/pw_async2/try.h",
        "public/pw_async2/waker.h",
        "public/pw_async2/waker_queue.h",
    ],
)
